package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.TestConstant;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityMarket;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityMarketGoods;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityMarketInfo;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityMarketMark;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * CREATE BY zlzxm ON 4/12/21
 */
public class VMWholesaleMarket extends ViewModel {

    public MutableLiveData<List<EntityMarket>> liveMarket = new MutableLiveData<>();

    public MutableLiveData<List<EntityMarketInfo>> liveMarketInfo = new MutableLiveData<>();

    public MutableLiveData<List<EntityMarket>> getLiveMarket() {
        return liveMarket;
    }

    public MutableLiveData<List<EntityMarketInfo>> getLiveMarketInfo() {
        return liveMarketInfo;
    }


    public void requestMarkets(){

        Observable.timer(2, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        liveMarket.setValue(createTestMarket());
                    }
                });



    }

    public void requestMarketInfo(){


        Observable.timer(2, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        liveMarketInfo.setValue(createTestMarketInfos());
                    }
                });

    }


    //<editor-fold defaultState="collapsed" desc="测试数据">


    private List<EntityMarket> createTestMarket(){

        List<EntityMarket> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {


            String pic =null ;

            if(i % 3== 0 ){

                pic = TestConstant.PIC_FIVE;

            }else  if(i % 3== 1 ){

                pic = TestConstant.PIC_SEVEN;
            }else if(i % 3 == 2 ){
                pic = TestConstant.PIC_SIX;

            }



            list.add(new EntityMarket(pic));
        }



        return  list;
    }


    private List<EntityMarketInfo> createTestMarketInfos(){

        List<EntityMarketInfo> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {


            List<EntityMarketMark> marks = new ArrayList<>();


            marks.add(new EntityMarketMark(TestConstant.PIC_EIGHT));
            marks.add(new EntityMarketMark(TestConstant.PIC_NINE));


            List<EntityMarketGoods> goods = new ArrayList<>();
            goods.add(new EntityMarketGoods(TestConstant.PIC_EIGHT));
            goods.add(new EntityMarketGoods(TestConstant.PIC_NINE));
            goods.add(new EntityMarketGoods(TestConstant.PIC_EIGHT));
            goods.add(new EntityMarketGoods(TestConstant.PIC_NINE));


            list.add(new EntityMarketInfo("商品"+i,marks,goods));

        }



        return  list;
    }

    // </editor-fold>
}
